/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.welfare.board;

import java.util.*;

import java.io.File;
import javax.servlet.*;
import javax.servlet.http.*;

import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;



/**
 *
 * @author eastar
 */
public class WordDeleteCommand implements Command
{

    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	HttpSession session = (HttpSession)request.getSession();

    	String userId	= StringUtil.nvl(session.getAttribute("session.user_id"));
    	String memberId = StringUtil.nvl(session.getAttribute("session.member_id"));

    	HashBox box = new HashBox(request);

		BoardDAO boardDAO = new BoardDAO();


		//수정권한 체크
		HashList boardInfo		= boardDAO.selectBoardInfo(box.getParam());
		String writeAuth		= boardInfo.get("write_auth",0);

		if ("A".equals(writeAuth)) {
			if ("".equals(userId)) {
				throw new Exception("로그인 후 삭제할 수 있습니다.");
			}
		} else if ("M".equals(writeAuth)) {
			if (!"".equals(userId)) {
				//관리자는 삭제가능
			} else if ("".equals(memberId)) {
				throw new Exception("로그인 후 삭제할 수 있습니다.");
			} else {
				HashList wordInfo = boardDAO.selectWordInfo(box.getParam());
				if (!memberId.equals(wordInfo.get("init_user",0))) {
					throw new Exception("본인글만 삭제할 수 있습니다.");
				}
			}
		} else {
			//비회원일 경우, 비밀번호를 비교하여 삭제여부를 결정한다.
		}

		
		//글삭제
		box.put("job_type", "D");


		boardDAO.saveWord(box.getParam());


		//결과 페이지 이동
		HashMap result		= new HashMap();
		result.put("result", 	"true");
		result.put("action",	"/board/wordList.jsp?board_id="+box.get("board_id"));

    	request.setAttribute("result", result);

    	return "/common/move_view.jsp";

    }
}
